/**
 * Created by yqj on 2017/2/17.
 */
const mongoose = require('mongoose'),
    Schema = mongoose.Schema;

const userSchema = new Schema({
    id: {type: Schema.Types.ObjectId},
    creatorFullName: {type: String, default: null},
    creatorRoleName: {type: String, default: null},
    enabled: {type: Boolean, default: true},
    fullName: {type: String},
    nickName: {type: String, default: null},
    portrait: {type: String, default: null},
    portrait_url: {type: String, default: null},
    rating: {type: String, default: null},
    subjects: {type: [String]},
    teachingExperience: {type: String},
    hasRole: {type: [Schema.Types.Mixed]},
    roleKey: {type: [Schema.Types.Mixed]},
    age: {type: Number, min: 18, index: true},
    phone: {type: Number, match: /^[1][3|5|8|7][0-9]{9}$/},
    loginAccount: {type: String},
    password: {type: String},
    email: {type: String, default: null},
    updated_at: {type: Date, default: Date.now()},
    created_at: {type: Date, default: Date.now()}
});

/**
 * 根据角色和账号前缀查询
 * @param role
 * @param prefix
 * @returns {T|*}
 */
userSchema.query.byRoleAndAccountPrefix = (role, prefix) => {
    console.log(role, prefix)
    if (prefix) {
        return this.find({
            role: role,
            loginAccount: new RegExp(prefix)
        });
    } else {
        return this.find({
            role: role
        });
    }
};

userSchema.query.byLimit = (limit) => {
    return this.limit(limit);
};

userSchema.query.getCount = (callback) => {
    return this.count(callback);
};




module.exports = userSchema;